import SimpleXMLRPCServer
import MySQLdb

class UserFields:
	Login=0
	Password=1
	PasswordLen=50
	Profile_Name=2
	Profile_NameLen=50
	About_Me=3
	About_MeLen=512
	Class_ID=4

class ClassFields:
	Class_ID=0
	Connected_Classes=1
	Teacher_Login=2
	New_User_Pass=3

class SQLCalls:
	def __init__(self):
		f=open('table.cfg','r')
		(ip,table,user,passw)=f.read().split(',')
   		self.__db = MySQLdb.connect(ip, user, passw, table)

	def __getUser(self, login):
		c=self.__db.cursor()
		c.execute("""SELECT * FROM User where Login = %s""",(login,))
		return c.fetchone()

	def __getClassID(self,user):
		return user[UserFields.Class_ID]

	def __parseProfile(self,user):
		return (user[UserFields.Profile_Name],user[UserFields.About_Me])

	def __isConnected(self,user,requested_user):
		if requested_user[UserFields.Class_ID] in self.__getClassConnections(user[4]):
			return True
		else:
			return False

	def __getClassConnections(self,ClassID):
		c=self.__db.cursor()
		c.execute("""SELECT * FROM Class WHERE Class_ID = %s""",(ClassID,))
		return c.fetchone()[ClassFields.Connected_Classes].split('&')

	def setProfileName(self,login,passw,name):
		user=self.__getUser(login)
		if user[UserFields.Password]==passw:
			if len(name) > UserFields.Profile_NameLen:
				name=name[0:UserFields.Profile_NameLen]
			c=self.__db.cursor()
			c.execute("""UPDATE User SET Profile_Name = %s WHERE Login= %s""",(name,login))
		else:
			Pass

	def setUserPass(self,login,passw,newPassW):
		user=self.__getUser(login)
		if user[UserFields.Password]==passw:
			#if len(newPassW) > UserFields.PasswordLen
			#THROW SOME SORT OF ERROR
			c=self.__db.cursor()
			c.execute("""UPDATE User SET Password = %s WHERE Login= %s""",(newPassW,login))
		else:
			Pass

	def setAboutMe(self,login,passw,aboutme):
		user=self.__getUser(login)
		if user[UserFields.Password]==passw:
			if len(name) > UserFields.About_MeLen:
				aboutme=aboutme[0:UserFields.About_MeLen]
			c=self.__db.cursor()
			c.execute("""UPDATE User SET About_Me = %s WHERE Login = %s""",(aboutme,login))
		else:
			Pass

	def createUser(self,newuserpass,login,passw,profilename):
		c=self.__db.cursor()
		c.execute("""SELECT * FROM Class WHERE New_User_Pass = %s""",(newuserpass,))
		ClassID = c.fetchone()[ClassFields.Class_ID]
		c.execute("""INSERT INTO User (Login,Password,Profile_Name,Class_ID) VALUES (%s,%s,%s,%s)""",(login,passw,profilename,ClassID))

	def getProfile(self, login, passw, requested_login):
		user=self.__getUser(login)
		if user[UserFields.Password]==passw:

			if login==requested_login:
				return self.__parseProfile(user)

			requested_user=self.__getUser(requested_login)		

			if self.__getClassID(user)==self.__getClassID(requested_user):
				return self.__parseProfile(requested_user)
			elif self.__isConnected(user,requested_user):
				return self.__parseProfile(requested_user)
			else:
				return None
		else:
			return None

server = SimpleXMLRPCServer.SimpleXMLRPCServer(("localhost", 8000), allow_none=True)
server.register_instance(SQLCalls())
server.serve_forever()
